/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package willispace;

import graphics.WindowManager;
import java.awt.GridLayout;
import java.awt.Point;
import javax.swing.JPanel;

/**
 * Render map
 * @author mateusz
 */
public class MapRenderer {

    /**
     * Reference to proper panel from fullScreenWindow
     */
    protected JPanel panel;
    private static int startTileX = 0;
    private static int startTileY = 0;
    private static Map map;

    /**
     * Creates new renderer with specified map
     * @param map   Map that should be rendered
     */
    public MapRenderer(Map map, JPanel panel) {
        MapRenderer.map = map;
        this.panel = panel;
        this.panel.setLayout(new GridLayout(Config.verticalTiles, Config.horizontalTiles, 0, 0));
        WindowManager.setMapSize(Config.tileWidth*Config.horizontalTiles, Config.tileHeight*Config.verticalTiles);
    }

    /**
     * Calculates tile indexes to pixels position
     * @param tileX     X (horizontal) index of tile
     * @param tileY     Y (vertical( index of tile
     * @return Point (0,0) of tile[x][y] (starting from 0)
     */
    public static Point tileToPixels(int tileX, int tileY) {
        return new Point(tileX * Config.tileWidth, tileY * Config.tileHeight);
    }

    /**
     * Calculates pixels position to tile indexes
     * @param x X (horizontal) coordinate
     * @param y Y (vertical( coordinate
     * @return indexes of tile containing point (x,y)
     */
    public static Point pixelsToTile(int x, int y) {
        return new Point(x / Config.tileWidth, y / Config.tileHeight);
    }

    /**
     * Calculates pixels position to tile indexes
     * @param p point to check
     * @return indexes of tile containing point p
     */
    public static Point pixelsToTile(Point p) {
        return pixelsToTile(p.x, p.y);
    }

    /**
     * Drawing Map
     */
    public void draw() {
        int horizontalTiles = Config.horizontalTiles;
        int verticalTiles = Config.verticalTiles;

        panel.removeAll();

        int currentTileX, currentTileY;

        // rysowanie tła
        for (int y = 0; y < verticalTiles; y++) {
            for (int x = 0; x < horizontalTiles; x++) {
                panel.add(map.getTile(x + startTileX, y + startTileY));
            }
        }
    }

    /**
     * Returns map offset
     * @return Pair (x,y) of map offset
     */
    public static Point getMapOffset() {
        return new Point(startTileX, startTileY);
    }

    /**
     * Moves map by specified coordinates
     * @param x horizontal move
     * @param y vertical move
     * @return  Direction object which specifies, what moves can be done now
     */
    public Directions<Boolean> moveMap(int x, int y) {
        startTileX += x;
        startTileY += y;

        Directions<Boolean> d = new Directions<Boolean>();
        d.left = (startTileX > 0);
        d.right = (startTileX + Config.horizontalTiles < map.getWidth());

        d.up = (startTileY > 0);
        d.down = (startTileY + Config.verticalTiles < map.getHeight());

        // notify all robots to update their locations
        WilliSpace.notifyAllRobots();

        return d;
    }
}
